package com.sooth.system.dict.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.sooth.common.core.constant.Constants;
import com.sooth.common.core.domain.R;
import com.sooth.common.log.annotation.Log;
import com.sooth.common.web.core.BaseController;
import com.sooth.system.dict.service.ISysDictDataService;
import com.sooth.system.dict.view.SysDictDataBo;
import com.sooth.system.dict.view.SysDictDataDto;
import com.sooth.system.dict.view.SysDictDataVo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典信息
 *
 * @author Lion Li
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/dict-data")
public class SysDictDataController extends BaseController {

    private final ISysDictDataService dictDataService;

    /**
     * 查询字典数据列表
     */
    @SaCheckPermission("system:dict:query")
    @GetMapping("/list")
    public R<List<SysDictDataVo>> list(SysDictDataDto dictData) {
        return R.ok(dictDataService.selectList(dictData));
    }


    /**
     * 查询字典数据详细
     *
     * @param id 字典code
     */
    @SaCheckPermission("system:dict:query")
    @GetMapping(value = "/query")
    public R<SysDictDataBo> query(@PathVariable Long id) {
        return R.ok(dictDataService.selectById(id));
    }


    /**
     * 新增字典数据
     */
    @SaCheckPermission("system:dict:add")
    @Log(title = "数据字典", message = "新增字典数据")
    @PostMapping("/add")
    public R<Void> save(@RequestBody SysDictDataBo dict) {
        return toAjax(dictDataService.insert(dict));
    }

    /**
     * 修改字典数据
     */
    @SaCheckPermission("system:dict:edit")
    @Log(title = "数据字典", message = "修改字典数据")
    @PutMapping("/edit")
    public R<Void> edit(@RequestBody SysDictDataBo dict) {
        return toAjax(dictDataService.updateById(dict));
    }

    /**
     * 删除字典数据
     */
    @SaCheckPermission("system:dict:remove")
    @Log(title = "数据字典", message = "删除字典数据")
    @DeleteMapping("/remove")
    public R<Void> remove(@RequestParam Long id) {
        return toAjax(dictDataService.deleteById(id));
    }

    @GetMapping("/os/list")
    public R<List<SysDictDataVo>> list(@RequestParam String dictType) {
        return R.ok(dictDataService.list(dictType, Constants.DEFAULT_TENANT_ID));
    }
}
